package model

import (
	"github.com/google/uuid"
	"gorm.io/gorm"
	hotel "hotel_3.27/common/proto"
	"hotel_3.27/variable"
)

type Order struct {
	gorm.Model
	HotelId int64   `gorm:"type:int;not null;index:ip_idx"`
	UserId  int64   `gorm:"type:int;not null;index:ip_idx"`
	OrderSn string  `gorm:"type:varchar(50);not null;index:ip_idx"`
	Num     int64   `gorm:"type:int;not null;index:ip_idx"`
	Price   float64 `gorm:"type:decimal(10,2);not null;index:ip_idx"`
	Pay     int64   `gorm:"type:int;not null;index:ip_idx"`
	Status  int64   `gorm:"type:int;not null;index:ip_idx"`
}

func (o Order) CreateHotel(in *hotel.OrderAddReq) (order *Order, err error) {

	h := &Hotel{}
	variable.DB.Find(&h)

	ordersn := uuid.New().String()

	order = &Order{
		HotelId: in.HotelId,
		UserId:  1,
		OrderSn: ordersn,
		Num:     in.Sum,
		Price:   h.Price * float64(in.Sum),
		Pay:     1,
		Status:  1,
	}

	err = variable.DB.Create(&order).Error
	if err != nil {
		return nil, err
	}
	return

}

func (o Order) FindHotel() (order []*Order, err error) {
	err = variable.DB.Find(&order).Error
	if err != nil {
		return nil, err
	}
	return
}
